package frobagroup.utils;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * Created by Tinsley on 2016/11/7.
 */
public class TinsleyCacheUtils {

    public static <K , V> LoadingCache<K , V> getLoadingCache(CacheLoader<K , V> cacheLoader) {
        return CacheBuilder.newBuilder().initialCapacity(Constants.INITIAL_CAPACITY).
                maximumSize(Constants.MAXIMUM_SIZE).concurrencyLevel(Constants.CONCURRENCY_LEVEL).
                expireAfterAccess(Constants.EXPIRE_AFTER_ACCESS, TimeUnit.MINUTES).recordStats().
                removalListener((e)->System.out.println(e.getKey() + " was removed, cause is " + e.getCause())).
                build(cacheLoader);
    }

    public static <K , V> Cache<K , V> getCache()
            throws ExecutionException, InterruptedException{
        return CacheBuilder.newBuilder().initialCapacity(Constants.INITIAL_CAPACITY).
                maximumSize(Constants.MAXIMUM_SIZE).concurrencyLevel(Constants.CONCURRENCY_LEVEL).
                expireAfterAccess(Constants.EXPIRE_AFTER_ACCESS, TimeUnit.MINUTES).recordStats().
                removalListener((e)->System.out.println(e.getKey() + " was removed, cause is " + e.getCause())).
                build();
    }

}
